package com.gzw.shop.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.daimajia.slider.library.SliderLayout;
import com.daimajia.slider.library.SliderTypes.BaseSliderView;
import com.daimajia.slider.library.SliderTypes.TextSliderView;
import com.gzw.shop.Contans;
import com.gzw.shop.R;
import com.gzw.shop.activity.WareListActivity;
import com.gzw.shop.adapter.HomeCatgoryAdapter;
import com.gzw.shop.bean.Banner;
import com.gzw.shop.bean.Compaign;
import com.gzw.shop.bean.HomeCampaign;
import com.gzw.shop.http.BaseCallBack;
import com.gzw.shop.http.OkHttpHelper;
import com.gzw.shop.http.SpotsCallBack;
import com.gzw.shop.widget.GToolbar;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;


public class HomeFragment extends BaseFragment {

    @BindView(R.id.toolbar)
    GToolbar toolbar;
    @BindView(R.id.slider)
    SliderLayout mSliderLayout;
    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;

    private HomeCatgoryAdapter mAdapter;

    private List<Banner> mBanners;

    private OkHttpHelper mHelper = OkHttpHelper.getInstance();


    @Override
    public int getLayoutId() {
        return R.layout.fragment_home;
    }

    @Override
    public void initView() {
        requestImages();
    }

    @Override
    public void initData() {
        initRecyclerView();
    }

    private void requestImages() {
        mBanners = new ArrayList<>();
        String url = "http://112.124.22.238:8081/course_api/banner/query";

        Map<String, Object> map = new HashMap<>();
        map.put("type", "1");

        mHelper.post(url, map, new SpotsCallBack<List<Banner>>(getActivity()) {
            @Override
            public void onSuccess(Response response, List<Banner> banners) {

                mBanners = banners;
                initSlider();
            }

            @Override
            public void onError(Response response, int code, Exception e) {

            }
        });

    }

    private void initSlider() {

        if (mBanners != null) {
            for (int i = 0; i < mBanners.size(); i++) {
                TextSliderView textSliderView = new TextSliderView(getActivity());
                textSliderView
                        .image(mBanners.get(i).getImgUrl())
                        .description(mBanners.get(i).getName())
                        .setScaleType(BaseSliderView.ScaleType.Fit);
                mSliderLayout.addSlider(textSliderView);
            }
        }
        mSliderLayout.setPresetTransformer(SliderLayout.Transformer.RotateUp);
        mSliderLayout.setDuration(4000);
    }

    private void initRecyclerView() {

        mHelper.get(Contans.API.CAMPAIGN_HOME, new SpotsCallBack<List<HomeCampaign>>(getContext()) {

            @Override
            public void onSuccess(Response response, List<HomeCampaign> campaigns) {

                mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

                mAdapter = new HomeCatgoryAdapter(campaigns, getActivity());
                mRecyclerView.setAdapter(mAdapter);

                mAdapter.setListener(new HomeCatgoryAdapter.OnCampaignClickListener() {
                    @Override
                    public void onClick(View view, Compaign compaign) {

//                        Toast.makeText(getActivity(), compaign.getTitle(), Toast.LENGTH_SHORT).show();

                        Intent intent = new Intent(getActivity(), WareListActivity.class);
                        intent.putExtra(Contans.CAMPAIGN_ID, compaign.getId());
//                        intent.putExtra("",);
                        startActivity(intent);

                    }
                });
            }

            @Override
            public void onError(Response response, int code, Exception e) {

            }
        });

    }

    @Override
    public void onDestroy() {
        if (mSliderLayout != null) {
            mSliderLayout.stopAutoCycle();
        }
        super.onDestroy();
    }

    public void showData() {
        if (mBanners.size() == 0) {
            requestImages();
        }
        if (mAdapter == null) {
            initRecyclerView();
        }
    }
}
